//
//  SwimDataModel.swift
//  TimeBoat
//
//  Created by Jun on 2022/9/8.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import Foundation

/// 游泳
@objcMembers class SwimDataModel: NSObject {
    /// 距离 U16 (单位:米)
    var distance: UInt16 = 0

    /// 摆臂次数 U16 目前是固定是 0
    var swingArm: UInt16 = 0

    /// 用时 U32 (单位:秒)
    var useTime: UInt32 = 0

    /// 起始经度 S32 (精度 0.00001 度)
    var startLng: UInt32 = 0

    /// 起始纬度 S32 (精度 0.00001 度)
    var startLat: UInt32 = 0

    /// 经度数组 S8[] (经度增量，精度 0.00001 度,每 5 秒钟记录一次数据)
    var lngArray: [UInt8]? = nil

    /// 纬度数组 S8[] (纬度增量，精度 0.00001 度,每 5 秒钟记录一次数据)
    var latArray: [UInt8]? = nil

    /// 速度数组 U8[] (单位:0.1km/h，每 5 秒钟记录一次数据)
    var speedArray: [UInt8]? = nil

    /// 心率数组 U8[] (单位:次/分钟，每分钟记录一次数据)//目前固定是 0
    var heartRateList: [UInt8]? = nil

    /// 温度 U16[] (每分钟记录一次数据)
    var temperatureList: [UInt16]? = nil

    /// 配速 U16[] (单位:秒, 每公里所用时间，每公里记录一次数据）
    var paceList: [UInt16]? = nil

    /// 卡路里 4Byte
    var calorie: UInt32 = 0
    
    convenience init(buf: [UInt8]) {
        self.init()
        
        var index = 0

        if (index + 2 <= buf.count) {
            // 里程 2Byte (单位:米)
            distance = UInt16.toUInt16(buf: buf, offset: index)
            index += 2
        }

        if (index + 2 <= buf.count) {
            // 摆臂次数 U16 目前是固定是 0
            swingArm = UInt16.toUInt16(buf: buf, offset: index)
            index += 2
        }

        if (index + 4 <= buf.count) {
            // 所用时间 4Byte (单位:秒)
            useTime = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            // 起始点经度
            startLng = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            // 起始点纬度
            startLat = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 2 <= buf.count) {
            // 经度数据数组 经度增量，精度 0.00001 度,每 5 秒钟记录一次数据
            let lngArrayLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (lngArrayLength > 0) {
                if (index + lngArrayLength <= buf.count) {
                    lngArray = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: lngArrayLength)
                    index += lngArrayLength
                } else {
                    Log.debug("index + lngArrayLength is overflow, \(index), \(lngArrayLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            // 纬度数据数组 纬度增量，精度 0.00001 度,每 5 秒钟记录一次数据
            let latArrayLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (latArrayLength > 0) {
                if (index + latArrayLength <= buf.count) {
                    latArray = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: latArrayLength)
                    index += latArrayLength
                } else {
                    Log.debug("index + latArrayLength is overflow, \(index), \(latArrayLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            // 速度数组
            let speedLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (speedLength > 0) {
                if (index + speedLength <= buf.count) {
                    speedArray = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: speedLength)
                    index += speedLength
                } else {
                    Log.debug("index + speedLength is overflow, \(index), \(speedLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            // 心率测量数组
            let heartRateLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (heartRateLength > 0) {
                if (index + heartRateLength <= buf.count) {
                    heartRateList = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: heartRateLength)
                    index += heartRateLength
                } else {
                    Log.debug("index + heartRateLength is overflow, \(index), \(heartRateLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            // 温度 U16[]
            let temperatureLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (temperatureLength > 0) {
                if (index + temperatureLength <= buf.count) {
                    temperatureList = [UInt16]()
                    for _ in 0...(temperatureLength-1) {
                        let data = UInt16.toUInt16(buf: buf, offset: index)
                        index += 2
                        temperatureList?.append(data)
                    }
                } else {
                    Log.debug("index + temperatureLength is overflow, \(index), \(temperatureLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            // 配速数组
            let paceLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (paceLength > 0) {
                if (index + paceLength <= buf.count) {
                    paceList = [UInt16]()
                    for _ in 0...(paceLength-1) {
                        let data = UInt16.toUInt16(buf: buf, offset: index)
                        index += 2
                        paceList?.append(data)
                    }
                } else {
                    Log.debug("index + paceLength is overflow, \(index), \(paceLength)")
                }
            }
        }

        if (index + 4 <= buf.count) {
            //卡路里
            calorie = UInt32.toUInt32(buf: buf, offset: index)
        }
    }
    
    @objc func toString() -> String {
        var str = self.yy_modelToJSONString()
        if nil == str {
            str = ""
        }
        
        return str!
    }
}
